package Easy;

import java.util.Random;

public class LC0374 {
    private static int guess(int guessNum) {
        long targetNum = 2147483647;
        if (targetNum < guessNum) return -1;
        else if (targetNum > guessNum) return 1;
        else return 0;
    }

    public static int guessNumber(int n) {
        long left = 1, right = (long) n + 1, mid; // left和right构成左闭右开区间；必须在计算前先对n进行转型，否则会溢出
        while (left < right) {
            mid = (left + right) / 2;
            if (guess((int) mid) == 0) return (int) mid;
            else if (guess((int) mid) < 0) right = mid;
            else left = mid + 1;
        }

        return -1; // 输入保证程序不会到达此位置
    }

    public static void main(String[] args) {
        System.out.println(guessNumber(2147483647));
    }
}
